package cn.edu.hqu.cst.chapter0204.web;

import cn.edu.hqu.cst.chapter0204.domain.Order;
import cn.edu.hqu.cst.chapter0204.domain.Taco;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Logger;


@Controller
@RequestMapping("/succeed")
public class SucceedController {
    private static final Logger log = (Logger) LoggerFactory.getLogger(SucceedController.class);

    public SucceedController() {
    }

    @GetMapping
    public String newsForm(Model model) {
        model.addAttribute("succeed", new Order());
        return "succeed";
    }

    @PostMapping
    @RequestMapping("/session")
    public String processNews(@SessionAttribute("order") Order order, @SessionAttribute("design") Taco design, HttpServletRequest request, Errors errors) {
        if (errors.hasErrors()) {
            return "succeed";
        } else {
            log.info("Login submitted: " + order);
            return "redirect:/";
        }
    }
}
